package com.itheima.servise.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.servise.StudentServise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiseImpl implements StudentServise {
    @Autowired
    private StudentMapper studentMapper;

    //学院列表查询
    @Override
    public PageBean list(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Student> rows = studentMapper.list(param);
        Page<Student> p = (Page<Student>) rows;
        return new PageBean(p.getTotal(), p.getResult());
    }

    //添加学员
    @Log
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }
    //根据id查询
    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }
    //违纪处理
    @Log
    @Override
    public void violation(Integer id, Short score) {
        //根据id查询学员违纪扣分并添加
        Student student = studentMapper.getById(id);
        Short violationScore = student.getViolationScore();
        short violationScore1 = (short) (violationScore + score);
        student.setViolationScore(violationScore1);
        //根据id查询学员违纪次数并++ 添加
        Short violationCount = student.getViolationCount();
        violationCount++;
        student.setViolationCount(violationCount);
        studentMapper.update(id, violationScore1, violationCount);

    }

    //修改学员信息
    @Log
    @Override
    public void putStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.putStudent(student);
    }

    //删除学员
    @Log
    @Override
    public void delete(String[] split) {
        studentMapper.delete(split);
    }


}
